package ch.hslu.weather;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;

public class TextHelper {

	private String string;

	public TextHelper(HttpResponse response) {
		string = BuildString(response);
	}

	public String SearchText(String pattern) {
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(string);
		if (m.find()) {
			return m.group(1);
		}
		return null;
	}

	private String BuildString(HttpResponse response) {
		InputStream in;
		StringBuilder sb = new StringBuilder();
		try {
			in = response.getEntity().getContent();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(in));
			String line = null;
			try {
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
			} catch (Exception ex) {

			} finally {
				try {
					in.close();
				} catch (Exception ex) {
				}
			}
		} catch (Exception e) {
		}
		return sb.toString();
	}
	
	public String toString() {
		return string;
	}

}